Import("utils_查看详情.js")

function isInMainPage_PPX() {
    var allres = ocrAllRes()
    if (!ocrTextMoreByRes(allres, '看视频再') && ocrTextMoreByRes(allres, '开心收下', '日常任务', '现金收益', '开宝箱得')) {
        log('在主页面了!')
        return true
    }

    return false
}

function clickCloseButtonPPX(closeButton) {
    if (!closeButton) {
        return
    }

    agentOrHidClick(closeButton, 0)
    agentClickRight(closeButton)

    commonClose通用X关闭()

}


function isInGGView_PPX() {
    log('判断是否在广告页面')
    var allres = ocrAllRes()

    if (ocrTextMoreCompleteByRes(allres, '广告', '广告1)', '广告|)')) {
        log('在广告页广告广告1)')
        sleep.second(1)
        return true
    }

    if (ocrTextMoreCompleteByRes(allres, '反馈', '跳过', '广告')) {
        log('在广告页反馈/跳过/广告')
        sleep.second(1)
        return true
    }

    if (ocrTextMoreByRes(allres, '继续观看', '直播间', '点击进入', '下滑', '记入奖励', '奖励时长', '看直播开宝箱')) {
        log('在广告页继续观看/直播间/点击进入')
        sleep.second(1)
        return true
    }

    var detects播音 = opencv.findImagesEx('图色681128.cv');
    if (detects播音 != null) {
        log('检测到播音图标')
        return true
    }

    log('不在广告页面!!!')
    return false
}

function watchGG_皮皮虾独享(uiObj, skipClick, maxTimes) {
    if (!uiObj) {
        log('无点击控件直接返回')
        return
    }
    if (maxTimes == undefined) {
        maxTimes = 12;
    }

    if (!skipClick) {
        agentOrHidClick(uiObj, 3)
    }

    log('判断是否在广告页面')
    if (ocrText('数据加载')) {
        sleep.second(5)
        if (ocrText('数据加载')) {
            sleep.second(5)
            if (ocrText('数据加载')) {
                sleep.second(5)
            }
        }
    }
    var isInGG = false
    whilestep((a, b, canbreak) => {
        var allres = ocrAllRes()
        if (isInGGView_PPX()) {
            log('在广告页面了')
            isInGG = true
            canbreak()
        }
    }, 10, 2)

    if (ocrWaitForText('完成A', '可获得奖励')) {
        var ocr1x = opencv.OCREx('图色251873.cv');
        if (ocr1x != null) {
            agentOrHidClick(ocr1x.getAllDetect()[0])
            agentOrHidClick(ocrText('关闭广告'))
            addExcuteFuncTimes('error_杂广告')
            return
        }
    }

    if (!isInGG) {
        log('不在广告页面再次执行点击操作')

        // 判断是否还在主页面
        if (isInMainPage_PPX()) {
            log('还在主页面再次点击原来的')
            agentOrHidClick(uiObj, 3)
            var is任然存在 = ocrText('看视频再', '看广告再', '看视频最高', '看广告最高', '看广告领', '看视频领', '看视频翻')
            if (is任然存在 == null) {
                is任然存在 = ocrText('翻倍领取', '看广告翻', '倍领取')
            }
            if (is任然存在) {
                agentOrHidClickOffectY(is任然存在, 10, 2)
            }
        } else {
            log('再次判断是否在广告页')
            if (!isInGGView_PPX()) {
                log('注意注意不在广告不在主页面')
                sleep.second(5)
            }
        }
    }

    // 是否是中断任务
    var allres = ocrAllRes()
    if (ocrTextMoreByRes(allres, '继续', '续观看', '放弃福利') && ocrTextMoreByRes(allres, '换一个', '直接退出', '坚持退出')) {
        // 继续观看
        log('继续观看')
        agentOrHidClick(ocrText('继续', '续观看', '放弃福利'))
    }

    // 判断广告类型
    // 获取广告时长
    log('获取广告时长')
    var is古籍广告 = false
    var isZbGG = false
    var isJuChang = false
    var isGameorDown = false
    var wbmaxTimes = 15
    var findtime = ocrWaitForMoreTextMillisecondInRegion(0, 0, 1, 0.16, 6, '秒', 's后', 's', '后')
    if (findtime) {
        var convertTime = getNumber(findtime.text())
        log('广告时长' + convertTime)
        sleep.second(1)
        if (convertTime > 60) {
            convertTime = 30
        }
        if (convertTime < 5) {
            convertTime = 15
        }

        var rgbs
        if (convertTime > 0) {
            log('循环检查')
            whilestep((stepTimes, alltimes, canBreak) => {
                rgbs = get界面采样点()
                var allres = ocrAllRes()
                log('剩余执行时间: ' + (convertTime - alltimes))

                if (ocrTextMoreByRes(allres, '直播间', '点击进入', '直播中', '进入直播')) {
                    isZbGG = true
                    // 直播间广告
                    log('直播间广告')
                    agentClickRightOffectRight(ocrTextMoreByRes(allres, '取消'), 10, 1, () => {
                        if (ocrWaitForMoreTextMillisecond('更多直播', '说点什么', '人气', '小时榜', 4)) {
                            agentOrHidback()
                            sleep.second(2)

                            // 返回后如果不在广告页面也不在主页面就再次返回
                            if (!isInGGView_PPX() && !isInMainPage_PPX()) {
                                agentOrHidback()
                                sleep.second(1)
                            }
                        }


                        // 如果不小心返回过了
                        allres = ocrAllRes()
                        if (ocrTextMoreByRes(allres, '继续', '续观看') && ocrTextMoreByRes(allres, '换一个', '直接退出', '坚持退出')) {
                            // 继续观看
                            log('继续观看')
                            agentOrHidClick(ocrText('继续', '续观看'))
                        }
                    })

                    canBreak()
                }
                // 判断是否是斗地主之类的
                if (ocrTextMoreByRes(allres, '斗地主', '当皇上', '种树', '快乐学习', '古籍', '芒果', '公益')
                    || ocrTextMoreByRes(allres, '种菜')) {
                    is古籍广告 = true
                    log('斗地主广告')
                    addExcuteFuncTimes('tt_斗地主_times')
                    canBreak()
                }

                if (ocrTextMoreByRes(allres, '手游', '侠', '仙', '游戏', '梦幻', '三国', '射击')
                    || ocrTextMoreByRes(allres, '传奇', '策略', '网易', '巨人', '三七', '蜀门')
                    || ocrTextMoreByRes(allres, '仙遇', '无尽', '原始', '征途', '热血', '江湖')
                    || ocrTextMoreByRes(allres, '寻道', '冰河', '冰雪', '灵兽', '武林', '飞飞')) {
                    log('游戏类广告')
                    isGameorDown = true
                    canBreak()
                }

                if ((ocrTextMoreByRes(allres, '下集', '剧', '剧场', '集', '观看') && !ocrTextMoreByRes(allres, '观看时长', '直播间', '计入奖励'))
                    || ocrTextMoreByRes(allres, '微信', '转微', '小游戏', '种菜', '跳过', '红果')) {
                    log('剧场类广告')
                    isJuChang = true
                }

                // 判断是否广告已经结束
                if (convertTime > 29 && alltimes > 30) {
                    if (ocrTextMoreByRes(allres, '已领取', '再看一', '开心收下', '领取成功', '领取奖励')) {
                        canBreak()
                    }
                }

            }, convertTime, 5, (canBreak) => {
                if (check界面是否不再变化(rgbs)) {
                    if (ocrText('领取成功')) {
                        log('界面不再变化!!!')
                        canBreak()
                    }
                }
            })


            log('循环检查结束')
        }

        if (is古籍广告) {
            log('古籍广告')
        }


        if (isGameorDown && !isZbGG) {
            log('游戏广或者下载广直接进入详情页')
            // ocrWaitForText('领取成功', 20, 2)
        }
    } else {
        wbmaxTimes = 30
    }

    // 是否去查看详情
    var allres = ocrAllRes()
    if (!is古籍广告 && !isJuChang
        && (isGameorDown)) {
        // 是否去查看详情
        var isGoDetail = Math.random() * 100 > 0
        if (isGoDetail) {
            // 直播间广告和其他广告
            // 游戏广和海外购直播/团购直播
            if (isZbGG) {
                // if (ocrTextMoreByRes(allres, '游戏', '下单', '优惠', '团购', '旗舰') || getExcuteFuncTimes('open_开宝箱_PPX_low') >= 1) {

                // }
                log('游戏或团购直播')
                addExcuteFuncTimes('tt_海外_times')
                agentOrHidClick(ocrTextMoreByRes(allres, '点击进入', '直播间', '去直播', '进入'), 2)

                if (getExcuteFuncTimes('tt_海外_times') % 5 == 0) {
                    go看直播挂机()
                } else {
                    go看直播浏览小手柄(convertTime > 30 ? convertTime - 15 : 15)
                }
                // 低金币次数3次以上会看直播, 每隔5个海外购进入看直播

                if (isIn直播界面_sample()) {
                    // 关闭直播
                    log('关闭直播')
                    var detects关闭直播 = opencv.findImagesEx('图色199710.cv');
                    if (detects关闭直播 != null) {
                        agentOrHidClick(detects关闭直播[0])
                    }

                    agentOrHidClick(ocrWaitForText('领取成功', '已成功领取奖励', '奖励已领取', '跳过'))
                    agentOrHidClick(ocrText('不使用', '坚决退出', '退出'))

                    if (isIn直播界面_sample()) {
                        log('还在直播界面??')
                        agentOrHidback()
                        sleep.second(2)

                        // 如果不小心直接到广告主界面
                        var allres = ocrAllRes()
                        if (ocrTextMoreByRes(allres, '继续', '续观看') && ocrTextMoreByRes(allres, '换一个', '坚持退出')) {
                            // 继续观看
                            log('继续观看')
                            agentOrHidClick(ocrText('继续', '续观看'))
                            closeButton = ocrWaitForText('领取成功', 20, 2)
                            clickCloseButtonPPX(closeButton)
                        } else {
                            agentOrHidClick(ocrTextMoreByRes(allres, '退出', '坚持', '坚决', '不使用'))
                        }
                    }
                }
            } else {
                viewDetail()
            }
        }
    } else {
        logSleep('非直播或游戏广不看详情')
    }

    // 等待广告结束或者等待视频的完播

    if (getRandomNum(0, 100) > 0) {
        var rgbs = get界面采样点()
        sleep.second(1)
        while (true) {
            if (check界面是否不再变化(rgbs) || ocrText('直播')) {
                log('完播界面不再变化!!!')
                break
            }

            wbmaxTimes = wbmaxTimes - 1
            if (wbmaxTimes < 0) {
                break
            }
            sleep.second(2)
            rgbs = get界面采样点()
        }
        // 进行完播
    }

    log('等待直到领取成功')
    var closeButton = ocrWaitForText('领取成功', '奖励已领取', 3, 2)
    if (closeButton == null) {
        var ocr1x = opencv.OCREx('图色251873.cv');
        if (ocr1x == null) {
            ocr1x = opencv.OCREx('图色132780.cv');
        }

        if (ocr1x != null) {
            closeButton = ocr1x.getAllDetect()[0]
        }
    }
    // 关闭广告
    if (closeButton) {
        log('点击关闭广告')
        sleep.second(1)
        // agentOrHidClick(closeButton)

        // agentClickRight(closeButton)
        clickCloseButtonPPX(closeButton)

        agentOrHidClick(ocrText('继续观看'), 1, () => {
            closeButton = ocrWaitForText('领取成功', '奖励已领取', 10, 3)
            if (closeButton == null) {
                var ocr1x = opencv.OCREx('图色251873.cv');
                if (ocr1x == null) {
                    ocr1x = opencv.OCREx('图色132780.cv');
                }

                if (ocr1x != null) {
                    closeButton = ocr1x.getAllDetect()[0]
                }
            }
            clickCloseButtonPPX(closeButton)
        })

        if (!ocrText('看视频再', '继续领取', '退出') && !isInMainPage_PPX()) {
            if (isInGGView_PPX()) {
                log('没有成功关闭广告!')
                sleep.second(3)
                agentClickRight(ocrTextMoreInRegion(0, 0, 1, 0.2, '领取成功', '已成功领取奖励'))
                sleep.second(3)
                if (isInGGView_PPX()) {
                    agentOrHidback()
                    sleep.second(2)
                }
            }
        }
    } else {
        log('没有关闭按钮')
        sleep.second(2)
        if (isInMainPage_PPX()) {
            log('已经再主页面了')
            sleep.second(1)
            return
        }

        if (!ocrText('看视频再领', '看视频再得', '看视频再')) {
            if (isInGGView_PPX()) {
                log('还在广告页准备返回')
                sleep.second(1)
                agentOrHidback()
                sleep.second(2)

                var ocr1x = opencv.OCREx('图色251873.cv');
                if (ocr1x != null) {
                    agentOrHidClick(ocr1x.getAllDetect()[0])
                }

                var allres = ocrAllRes()
                if (ocrTextMoreByRes(allres, '继续', '续观看', '抓住奖励', '奖励机会') && ocrTextMoreByRes(allres, '换一个', '坚持退出', '直接退出')) {
                    // 继续观看
                    log('继续观看')
                    if (!isJuChang) {
                        agentOrHidClick(ocrText('继续', '续观看', '抓住奖励', '奖励机会'))

                    }

                    closeButton = ocrWaitForText('领取成功', 20, 2)
                    if (closeButton == null) {
                        var ocr1x = opencv.OCREx('图色251873.cv');
                        if (ocr1x != null) {
                            closeButton = ocr1x.getAllDetect()[0]
                        }
                    }
                    clickCloseButtonPPX(closeButton)

                } else {
                    // 存在广告页的二次弹窗
                    if (isInGGView_PPX()) {
                        log('异常2')
                        agentOrHidback()
                        sleep.second(2)
                    } else {
                        if (isInMainPage_PPX()) {
                            return
                        }
                        agentOrHidback()
                        sleep.second(2)
                        closeButton = ocrWaitForText('领取成功', 10, 2)
                        if (closeButton == null) {
                            var ocr1x = opencv.OCREx('图色251873.cv');
                            if (ocr1x != null) {
                                closeButton = ocr1x.getAllDetect()[0]
                            }
                        }
                        clickCloseButtonPPX(closeButton)
                    }
                }

            } else {
                log('自动在详情页面?')
                // if (ocrText('立即下载', '开发者', '应用权限')) {
                //     agentOrHidback()
                //     sleep.second(2)
                // }
                agentOrHidback()
                log('已返回按钮')
                sleep.second(1)
                // sleep.second(2)
                log('等待按钮领取成功')
                closeButton = ocrWaitForText('领取成功', 10, 2)
                if (closeButton == null) {
                    if (isInMainPage_PPX()) {
                        return
                    }
                    agentOrHidback()
                    closeButton = ocrWaitForText('领取成功', 3, 2)

                }
                if (closeButton == null) {
                    var ocr1x = opencv.OCREx('图色251873.cv');
                    if (ocr1x == null) {
                        ocr1x = opencv.OCREx('图色132780.cv');
                    }

                    if (ocr1x != null) {
                        closeButton = ocr1x.getAllDetect()[0]
                    }
                }
                clickCloseButtonPPX(closeButton)
            }
        }

    }

    logSleep('皮皮虾再次看多休息3秒', 3)
    // 判断是任务中心还是再来一次
    var allres = ocrAllRes()

    // 如果是任务中心结束迭代
    if (isInMainPage_PPX()) {
        log('在主页面了2')
        sleep.second(1)
        return
    }

    if (maxTimes <= 0) {
        log('达到看广告极限次数')
        agentOrHidClick(ocrTextMoreByRes(allres, '退出', '残忍离开', '先不看', '坚持', '放弃', '强制'))
        return
    }

    log('去判断再来一次')
    var moreFlagAgain = ocrTextMoreByRes(allres, '看视频再领', '看视频再得', '看视频再')
    if (moreFlagAgain) {
        // 判断金币是否达标
        // ocrTextTrue('5')
        var jbNum = getDectNumber(moreFlagAgain)

        log(`获取金币: ${jbNum}`)
        if (jbNum > 0 && jbNum < 1500) {
            log('金币不达标退出')

            addExcuteFuncTimes('open_开宝箱_PPX_low')
            agentOrHidClick(ocrTextMoreByRes(allres, '直接退出', '坚持退', '持退出'))
            return
        } else {
            if (jbNum >= 4000 || jbNum == 3000 || jbNum == 2000) {
                addExcuteFuncTimes('open_开宝箱_PPX_low', 0)
            }
        }

        log('再看一次')
        // 如果是再来一次
        watchGG_皮皮虾独享(moreFlagAgain, false, maxTimes - 1)
    }

    log('最后检测一次是否还在广告内')
    if (isInGGView_PPX()) {
        log('还在广告内强制退出')
        agentOrHidback()
        sleep.second(2)
        agentOrHidClick(ocrText('退出', '残忍离开', '先不看', '坚持', '放弃', '强制'))
    }

    agentOrHidClick(ocrText('继续', '续观看', '抓住奖励', '奖励机会'), 2, () => {
        sleep.second(30)
        var closeBtn = ocrText('领取成功')
        if (closeBtn == null) {
            var ocr1x = opencv.OCREx('图色251873.cv');
            if (ocr1x == null) {
                ocr1x = opencv.OCREx('图色132780.cv');
            }

            if (ocr1x != null) {
                closeBtn = ocr1x.getAllDetect()[0]
            }
        }
        clickCloseButtonPPX(closeBtn)
    })

    log('结束广告')
}